﻿<!-- 正文开始 -->
<style >
     .layui-table-cell{
         height: auto!important;
         white-space: normal;
     }
</style>

<!-- 正文开始 -->
<div class="layui-fluid" style="height: calc(100% - 30px);">
    <div class="layui-row layui-col-space15" style="height: 100%;">
        <!-- 左树 -->
        <div class="layui-col-sm12 layui-col-md4 layui-col-lg3" style="height: 100%;">
            <div class="layui-card" style="height: 100%;">
            	<div class="layui-card-header">分类</div>
                <div class="layui-card-body mini-bar" id="treeTbTree" style="height: calc(100% - 150px)">
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-input-inline mr0">
                                    <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="form-search-icon-classify-btn" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button class="layui-btn icon-btn" id="add-icon-classify-btn">
                                    <i class="layui-icon">&#xe654;</i>添加
                                </button>                                
                            </div>
                        </div>
                    </div>                
                	<table class="layui-table" id="table-src-icon-classify" lay-filter="table-src-icon-classify"></table>
                </div>
            </div>
        </div>
        <!-- 右表 -->
        <div class="layui-col-sm12 layui-col-md8 layui-col-lg9">
            <div class="layui-card">
            	<div class="layui-card-header">图标列表</div>
                <div class="layui-card-body" >
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-input-inline mr0">
                                    <input name="name" class="layui-input" type="text" placeholder="输入关键字"/>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="form-icon-search-btn" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button class="layui-btn icon-btn" id="upload-icon-btn">
                                    <i class="layui-icon">&#xe681;</i>上传
                                </button>
                            </div>
                        </div>
                    </div>
                    <table class="layui-table" id="tableIcon" lay-filter="tableIcon"></table>
                </div>
            </div>
        </div>

    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarIconClassify">
#[[
  {{#  if(d.id === '1' ){ }}
	<span>系统保留</span>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  {{#  } }}
]]#
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="icon-classify-form-script">
    <form id="icon-classify-form" lay-filter="icon-classify-form" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50" lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入标识" type="text" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入序号" type="number" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="submit-icon-classify-filter" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarIcon">
    <a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'util', 'admin','upload','zTree','treeSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var upload = layui.upload;
        var classifyId='';
        // 渲染表格
        form.render('select');
        // 渲染表格
        var classifyTb = table.render({
            elem: '#table-src-icon-classify',
            url: '#(ctx)/resources/icon/listDataForClassify',
            page: true,
            cellMinWidth: 100,
            height: 'full-290',
            cols: [[
                {type:'radio'},
                {field: 'name', sort: false, title: '名称'},
                {align: 'center', toolbar: '#tableBarIconClassify', title: '操作'}
            ]]
        });
        
        // 搜索
        form.on('submit(form-search-icon-classify-btn)', function (data) {
        	classifyTb.reload({where: data.field}, 'data');
        });

        // 添加
        $('#add-icon-classify-btn').click(function () {
        	showClassifyEditModel();
        });
        
        table.on('row(table-src-icon-classify)', function(obj){
		  	classifyId = obj.data.id;
		  	insTb.reload({where: {classifyId:obj.data.id}}, 'data');
		});
        
        table.on('tool(table-src-icon-classify)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	showClassifyEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDelClassify(obj);
            }
        });
        
        
        var insTb = table.render({
            elem: '#tableIcon',
            url: '#(ctx)/resources/icon/listData',
            page: true,
            cols: [[
                {type: 'numbers'},
                {
                    align: 'center', templet: function (d) {
                    	if(d.url)
                        	return '<img src="#(ctx)'+d.url+'" />';
                        else
                        	return '';
                    }, title: '缩略图', width: 100, unresize: true
                },
                {field: 'name', sort: false, title: '名称'},
                {field: 'width', sort: false, title: '宽度'},
                {field: 'height', sort: false, title: '高度'},
                {align: 'center', toolbar: '#tableBarIcon',width:120, title: '操作'}
            ]]
        });

		upload.render({
      		elem: '#upload-icon-btn',
      		url: '#(ctx)/resources/icon/upload',
      		accept: 'images',
      		data: {
    			classifyId: function(){
    				if(classifyId && classifyId!=undefined){
    					return classifyId;
    				}
    				return "";
    			}
    		},
      		done: function(res, index, upload) {
      			layer.closeAll('loading'); //关闭loading
      			if (200 == res.code) {
      				layer.msg(res.message, {
      					icon: 1
      				});
      				insTb.reload();
      			} else {
      				layer.msg(res.message, {
      					icon: 2
      				});
      			}
      		},
      		error: function(index, upload) {
      			layer.closeAll('loading');
      			layer.msg("上传失败！", {
      				icon: 2
      			});
      		}
      	});

        // 搜索
        form.on('submit(form-icon-search-btn)', function (data) {
        	data.field.classifyId = classifyId;
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableIcon)', function (obj) {	
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') { // 删除
                doDel(obj);
            }
        });
       	
        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“【<span style="color:red;">' + obj.data.name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('resources/icon/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (200==res.code) {
                        layer.msg(res.message, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
        
        function doDelClassify(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”类型吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('resources/icon/deleteClassify', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
        
        // 显示编辑弹窗
        function showClassifyEditModel(mServerMap) {
            admin.open({
                type: 1,
                offset: 't',
                title: (mServerMap ? '修改' : '添加') + '服务',
                content: $('#icon-classify-form-script').html(),
                success: function (layero, dIndex) {
                    var url = 'resources/icon/saveClassify';
                    form.val('icon-classify-form', mServerMap);  // 回显数据
                    // 表单提交事件
                    form.on('submit(submit-icon-classify-filter)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['iconClassify.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                classifyTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }
        
    });
    

</script>